#include "common/api/server.h"
#include "common/api/server_helper.hpp"

#include "api_controller.h"

#include "get_elements_handler.h"

namespace Api
{

GetElementsHandler::GetElementsHandler(crow::App<>& crowApplication, ApiController* controller)
    : m_controller(controller)
{
    ADD_CROW_ROUTE_GET_ELEMENTS(
        crowApplication,
        [this](
            const Uuids::Uuid& sessionUuid,
            Elements::ElementType type,
            const std::vector<unsigned>& ids
        ) -> std::vector<std::shared_ptr<Elements::Element>>
        {
            // TODO : добавить проверку прав
            std::vector<std::shared_ptr<Elements::Element>> result;
            const auto elements = m_controller->dataController()->elements(type, ids);
            result.reserve(elements.size());
            for (auto dataElement : elements)
                result.push_back(dataElement->element());
            m_controller->changesManager().erase(sessionUuid, type, ids);
            return result;
        }
    );
}

} // namespace Api
